<template>
    <div>
        <el-container>
            <el-main>
                <el-row>
                    <el-col :offset="11" v-show="!sData.isNull">
                        <span>创建时间：{{this.sData.form.ctime | unix2Date}}</span>-<span>修改时间：{{this.sData.form.mtime | unix2Date}}</span>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col>
                        <el-form :model="sData.form" :rules="sData.rules" ref="sForm" v-loading="sData.loading">
                            <el-form-item prop="content">
                                <quill-editor v-model="sData.form.content"
                                              ref="myQuillEditor"
                                              :options="editorOption"
                                              style="height: 500px"
                                              @blur="onEditorBlur($event)"
                                              @focus="onEditorFocus($event)"
                                              @ready="onEditorReady($event)">
                                </quill-editor>
                            </el-form-item>
                        </el-form>
                    </el-col>
                </el-row>
            </el-main>
            <el-footer>
                <el-row>
                    <el-col :span="5" :offset="10">
                        <el-button size="mini" type="info" v-show="!this.sData.isNull" @click="handleEdit">保存</el-button>
                        <el-button size="mini" type="primary" v-show="this.sData.isNull" @click="handleAdd">添加</el-button>
                        <el-button size="mini" type="danger" v-show="!this.sData.isNull" @click="handleDelete">删除</el-button>
                    </el-col>
                </el-row>
            </el-footer>
        </el-container>
    </div>
</template>

<script>
    export default {
        data () {
            return {
                sData: {
                    loading: false,
                    isNull: false,
                    form: {
                        id:-1,
                        content: '<h2>暂无内容</h2>',
                        ctime:undefined,
                        mtime:undefined,
                    },
                    rules: {
                        content: [
                            { required: true, message: '请输入内容', trigger: 'blur' },
                            { min: 3, max:300, message: '长度于 3-300 个字符', trigger: 'blur' },
                        ]
                    },
                },
                editorOption: {

                },
                formLabelWidth: '120px'
            }
        },
        // manually control the data synchronization
        // 如果需要手动控制数据同步，父组件需要显式地处理changed事件
        methods: {
            onEditorBlur(quill) {
                console.log('editor blur!', quill)
            },
            onEditorFocus(quill) {
                console.log('editor focus!', quill)
            },
            onEditorReady(quill) {
                console.log('editor ready!', quill)
            },
            //添加权利与义务
            handleAdd(){
                const _this = this;
                this.$refs.sForm.validate((valid) => {
                    if (valid) {
                        _this.sData.loading=true;
                        let rightObligationVo=_this.sData.form;
                        this.postRequestJson("/rightobligation/districtHeadSave",rightObligationVo)
                            .then(resp =>{
                                if (resp && resp.data && resp.data.code === '200') {
                                    _this.sData.loading=false;
                                    this.getRightObligationByDistrictHead();
                                }else {
                                    _this.sData.loading=false;
                                }
                            })
                    }
                });
            },
            //修改
            handleEdit() {
                //提交权利与义务修改信息
                const _this = this;
                this.$refs.sForm.validate((valid) => {
                    if (valid) {
                        _this.sData.loading=true;
                        let rightObligationVo=_this.sData.form;
                        this.putRequestJson("/rightobligation/u",rightObligationVo)
                            .then(resp =>{
                                if (resp && resp.data && resp.data.code === '200') {
                                    _this.sData.loading=false;
                                    this.getRightObligationByDistrictHead();
                                }else {
                                    _this.sData.loading=false;
                                }
                            })
                    }
                });
            },
            //删除
            handleDelete(){
                //提交权利与义务删除信息
                const _this = this;
                _this.sData.loading=true;
                let ids=[];
                ids.push(_this.sData.form.id);
                this.deleteRequest("/rightobligation/d/"+ids)
                    .then(resp =>{
                        if (resp && resp.data && resp.data.code === '200') {
                            _this.sData.loading=false;
                            _this.sData.isNull=true;
                            _this.sData.form={
                                id:-1,
                                content: '<h2>暂无内容</h2>',
                                ctime:undefined,
                                mtime:undefined,
                            }
                            _this.getRightObligationByDistrictHead();
                        }else {
                            _this.sData.loading=false;
                        }
                    })
            },
            getRightObligationByDistrictHead(){
                const _this = this;
                _this.sData.loading=true;
                this.getRequest("/rightobligation/districtHeadGetRightObligation")
                    .then(resp =>{
                        if (resp && resp.data && resp.data.code === '200') {
                            const data = resp.data;
                            if (data.result){
                                _this.sData.form=data.result;
                                _this.sData.isNull=false;
                            }else {
                                _this.sData.isNull=true;
                            }
                            _this.sData.loading=false;
                        }else {
                            _this.sData.loading=false;
                        }
                    })
            }
        },
        computed: {
            editor() {
                return this.$refs.myQuillEditor.quill
            }
        },
        mounted() {
            console.log('this is current quill instance object', this.editor);
            this.getRightObligationByDistrictHead();
        },
        filters:{
            unix2Date: function (unixDate) {
                if (unixDate){
                    let date = new Date(unixDate);
                    let y = date.getFullYear();
                    let MM = date.getMonth() + 1;
                    MM = MM < 10 ? ('0' + MM) : MM;
                    let d = date.getDate();
                    d = d < 10 ? ('0' + d) : d;
                    let h = date.getHours();
                    h = h < 10 ? ('0' + h) : h;
                    let m = date.getMinutes();
                    m = m < 10 ? ('0' + m) : m;
                    let s = date.getSeconds();
                    s = s < 10 ? ('0' + s) : s;
                    return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;
                }
            },
        }
    }
</script>
<style scoped>

</style>
